package org.googlecode.lightning.search.parse;

import java.io.IOException;
import java.io.Serializable;

import org.daragon.fulltext.parse.parser.Parser;
import org.daragon.fulltext.segment.Holder;
import org.daragon.fulltext.segment.HolderMaker;

/**
 * 
 * @author daragon
 * 
 */
public class ParseTool {

	private Parser parser;
	private HolderMaker holdMaker;

	public HolderMaker getHoldMaker() {
		return holdMaker;
	}

	public void setHoldMaker(HolderMaker holdMaker) {
		this.holdMaker = holdMaker;
	}

	public ParseTool(Parser parser) {
		super();
		this.parser = parser;
	}

	/**
	 * 
	 * @param path
	 * @return
	 * @throws IOException
	 */
	public String handle(String path) {
		Holder holder = null;
		try {
			holder = holdMaker.hold(path);
		} catch (IOException e) {
			e.printStackTrace();
		}
		String text = parser.parse(path, holder);
		return text;
	}

	/**
	 * 
	 * @param id
	 * @param filename
	 * @param content
	 * @return
	 */
	public String handle(Serializable id, String filename, byte[] content) {
		Holder holder = null;
		try {
			holder = holdMaker.hold(id, filename, content);
		} catch (IOException e) {
			e.printStackTrace();
		}
		String text = parser.parse(null, holder);
		return text;
	}

}
